﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace D2D2.Core
{
    public enum SpriteType
    {
        Blue = 1,
        Green = 2,
        Chest = 3
    }

    public abstract class BattleSprite
    {
        protected static readonly Dictionary<SpriteType, Rectangle> _spriteList =
            new Dictionary<SpriteType, Rectangle>();

        protected static Texture2D _monsterTexture;
        protected static Texture2D _chestTexture;
        protected static Texture2D _liveTexture;
        protected static Texture2D _dropTexture;
        protected static Texture2D _fightTexture;

        protected Rectangle _initialFrame;
        protected ProgressManager _progress;
        protected Texture2D _texture;
        protected bool _init;
        protected int _frameCount;
        protected readonly List<Rectangle> _frames = new List<Rectangle>();
        protected SpriteType _spriteType;
        
        protected readonly Random _rand = new Random(DateTime.Now.Millisecond);

        static BattleSprite()
        {
            _spriteList.Add(SpriteType.Blue, new Rectangle(0, 0, 90, 88));
            _spriteList.Add(SpriteType.Green, new Rectangle(0, 88, 72, 88));
            _spriteList.Add(SpriteType.Chest, new Rectangle(0, 0, 120, 90));
        }

        public static void LoadTextures(Texture2D monsterTexture, Texture2D chestTexture, Texture2D liveTexture, Texture2D dropTexture, Texture2D fightTexture)
        {
            _monsterTexture = monsterTexture;
            _chestTexture = chestTexture;
            _liveTexture = liveTexture;
            _dropTexture = dropTexture;
            _fightTexture = fightTexture;
        }

        public static Texture2D DropTexture { get { return _dropTexture; } }

        /*
        public static void LoadCurves(List<CurveInfo> dropCurves)
        {
            _dropCurves = dropCurves;
        }
        */

        protected BattleSprite(Rectangle location, SpriteType spriteType)
        {
            Location = location;
            _spriteType = spriteType;
            _initialFrame = _spriteList[spriteType];
            _frameCount = GetFrameCount();

            Rectangle progressLocation = new Rectangle(location.X, location.Y - 15, location.Width, 10);
            _progress = new ProgressManager(_liveTexture, progressLocation);

            Alive = true;
        }

        /// <summary>
        /// Сколько жизней прирастает за 0.1 секунды
        /// </summary>
        public float LiveRating { get; set; }

        /// <summary>
        /// Жив ли объект
        /// </summary>
        public bool Alive { get; protected set; }

        /// <summary>
        /// Местоположение объекта
        /// </summary>
        public Rectangle Location { get; private set; }

        /// <summary>
        /// Предельное количество жизней
        /// </summary>
        public float TotalLive { get; private set; }

        public bool MustResponse { get; set; }

        public Rectangle GetInitialFrame(SpriteType spriteType)
        {
            return _spriteList[spriteType];
        }

        protected void LoadFrames()
        {
            for (int x = 1; x < _frameCount; x++)
            {
                _frames.Add(new Rectangle(
                                _initialFrame.X + (_initialFrame.Width*x),
                                _initialFrame.Y,
                                _initialFrame.Width,
                                _initialFrame.Height));
            }
        }

        public abstract void Update(GameTime gameTime);
        public abstract void Draw(SpriteBatch spriteBatch);
        public abstract void DecreaseHit();
        protected abstract int GetFrameCount();

        public void Init(float totalLive)
        {
            TotalLive = totalLive;
            LoadFrames();

            _init = true;
        }


    }
}
